﻿using Microsoft.EntityFrameworkCore.Storage;
 
namespace Kernelx.Contexts.Repositories
{
    using Core;

    internal class Transaction : ITransaction
    {
        private IDbContextTransaction _dbContextTransaction;
        public Transaction(IDbContextTransaction transaction)
        {
            _dbContextTransaction = transaction;
        }

        public void AutoCommit()
        {
            try
            {
                _dbContextTransaction?.Commit();
                _dbContextTransaction = null;
            }
            catch
            {
                Rollback();
                throw;
            }
        }

        public void Dispose()
        {
            AutoCommit();
            _dbContextTransaction = null;
        }

        public void Rollback()
        {
            _dbContextTransaction.Rollback();
        }
    }
}
